const express = require('express')
const db = require('../../utils/db')
const router = express.Router()
//修改权限
router.post('/editPower', (req, res) => {
  const power = [
    { title: '经营概况', icon: 'AccountBookOutlined', path: '/Home/Overview' },
    { title: '新开就诊', icon: 'FileAddOutlined', path: '/Home/NewVisit' },
    {
      title: '挂号管理',
      icon: 'CheckSquareOutlined',
      children: [
        { title: '新增挂号', path: '/Home/RegisteredAdd' },
        { title: '挂号记录', path: '/Home/Registered' }
      ]
    },
    {
      title: '药品零售',
      icon: 'ShoppingOutlined',
      path: '/Home/RetailMedicine'
    },
    {
      title: '收费管理',
      icon: 'MoneyCollectOutlined',
      path: '/Home/ChargeManagement'
    },
    {
      title: '患者管理',
      icon: 'TeamOutlined',
      path: '/Home/PatientManagement'
    },
    {
      title: '药品管理',
      icon: 'MedicineBoxOutlined',
      children: [
        { title: '药品调价', path: '/Home/pricing' },
        { title: '药品信息维护', path: '/Home/DrugInformation' }
      ]
    },
    {
      title: '统表管理',
      icon: 'AreaChartOutlined',
      children: [
        { title: '药品统计', path: '/Home/DrugStatistics' },
        { title: '患者统计', path: '/Home/PatienStatistics' },
        { title: '收费统计', path: '/Home/ChargeStatistics' }
      ]
    },
    {
      title: '系统管理',
      icon: 'SettingOutlined',
      children: [
        { title: '费用设置', path: '/Home/ExpensesSetting' },

        { title: '员工管理', path: '/Home/EmployeesManagement' }
      ]
    }
  ]
  const id = 1001
  const sql = `update role set r_power='${JSON.stringify(
    power
  )}' where r_id=${id};`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ error: 1, msg: '数据库查询失败', err })
    } else {
      res.send({ error: 0, msg: 'ok' })
    }
  })
})
//获取员工列表
router.get('/getUserList', (req, res) => {
  const { name, current, size } = req.query
  const sql = `SELECT * FROM doctor JOIN role WHERE u_name LIKE '%${name}%' AND u_isDel=0 AND u_r_id=r_id LIMIT ${
    (current - 1) * size
  },${size};`
  const totalSql = `SELECT COUNT(u_id) AS total FROM doctor WHERE u_name LIKE '%${name}%' AND u_isDel=0;`
  db.query(sql, (err, data) => {
    db.query(totalSql, (err2, data2) => {
      {
        if (err || err2) {
          res.send({ error: 1, msg: '数据库查询失败', err })
        } else {
          res.send({
            error: 0,
            msg: '数据获取成功',
            data,
            total: data2[0].total
          })
        }
      }
    })
  })
})
//获取角色列表
router.get('/getRoleList', (req, res) => {
  const { name, current, size } = req.query
  const sql = `SELECT * FROM role WHERE r_name LIKE '%${name}%' AND r_isDel=0 LIMIT ${
    (current - 1) * size
  },${size};`
  const totalSql = `SELECT COUNT(r_id) AS total FROM role WHERE r_name LIKE '%${name}%' AND r_isDel=0;`
  db.query(sql, (err, data) => {
    db.query(totalSql, (err2, data2) => {
      {
        if (err || err2) {
          res.send({ error: 1, msg: '数据库查询失败', err })
        } else {
          res.send({
            error: 0,
            msg: '数据获取成功',
            data: data.map(v => {
              v.r_power = v.r_power.split(',')
              return v
            }),
            total: data2[0].total
          })
        }
      }
    })
  })
})
//删除员工
router.post('/delEmployees', (req, res) => {
  const { id } = req.body
  sql = `UPDATE doctor SET u_isDel=1 WHERE u_id=${id};`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({
        error: 1,
        msg: '数据库操作失败',
        sql
      })
    } else {
      res.send({
        error: 0,
        msg: '删除成功'
      })
    }
  })
})
//删除角色
router.post('/delRole', (req, res) => {
  const { id } = req.body
  sql = `UPDATE role SET r_isDel=1 WHERE r_id=${id};`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({
        error: 1,
        msg: '数据库操作失败',
        sql
      })
    } else {
      res.send({
        error: 0,
        msg: '删除成功'
      })
    }
  })
})
//获取角色分类
router.get('/getRoles', (req, res) => {
  const sql = `select r_id as id,r_name as name from role WHERE r_isDel=0 group by r_name;`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({
        error: 1,
        msg: '数据库操作失败',
        sql
      })
    } else {
      res.send({
        error: 0,
        msg: '获取数据成功',
        data
      })
    }
  })
})
//添加员工
router.post('/addEmployess', (req, res) => {
  const {
    EmployeesName,
    address,
    age,
    cardId,
    email,
    password,
    role,
    sex,
    status,
    tel,
    time
  } = req.body
  const sql = `INSERT INTO clinic.doctor (u_id, u_name, u_sex, u_age, u_tel, u_r_id, u_createTime, u_status, u_email, u_cardID, u_address, u_password, u_isDel) VALUES (NULL, '${EmployeesName}', '${sex}', ${age}, ${tel}, ${role}, '${time}', ${status}, '${email}', '${cardId}', '${address}', '${password}', 0);`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({
        error: 1,
        msg: '数据库操作失败',
        sql
      })
    } else {
      res.send({
        error: 0,
        msg: '保存成功'
      })
    }
  })
})
//编辑员工
router.post('/editEmployess', (req, res) => {
  const {
    EmployeesName,
    address,
    age,
    cardId,
    email,
    password,
    role,
    sex,
    status,
    tel,
    id
  } = req.body
  const sql = `UPDATE clinic.doctor SET u_name = '${EmployeesName}', u_sex = '${sex}', u_age = ${age}, u_tel = ${tel}, u_r_id = ${role}, u_status = ${status}, u_email = '${email}', u_cardID = '${cardId}', u_address = '${address}', u_password = '${password}' WHERE u_id = ${id};`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({
        error: 1,
        msg: '数据库操作失败',
        sql
      })
    } else {
      res.send({
        error: 0,
        msg: '保存成功'
      })
    }
  })
})
//添加角色
router.post('/addRole', (req, res) => {
  const { roleName, time, des, power } = req.body
  const sql = `INSERT INTO clinic.role (r_id, r_name, r_des, r_createTime, r_power, r_isDel) VALUES (NULL, '${roleName}', '${des}', '${time}','${power}', 0);`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ error: 1, msg: '数据库查询失败', err })
    } else {
      res.send({ error: 0, msg: '操作成功' })
    }
  })
})
//编辑角色
router.post('/editRole', (req, res) => {
  const { roleName, des, power, id } = req.body
  const sql = `UPDATE clinic.role SET r_name = '${roleName}', r_des = '${des}',  r_power = '${power.join(
    ','
  )}' WHERE r_id = ${id};`
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ error: 1, msg: '数据库查询失败', err })
    } else {
      res.send({ error: 0, msg: '操作成功' })
    }
  })
})
//修改个人资料
router.post('/updateProfile', (req, res) => {
  const { EmployeesName, age, sex, tel, email, cardId, address, id } = req.body
  const sql = `UPDATE clinic.doctor SET u_name = '${EmployeesName}', u_sex = '${sex}', u_age = ${age}, u_tel = ${tel},u_email = '${email}', u_cardID = '${cardId}', u_address = '${address}' WHERE u_id = ${id};`
  db.query(sql, err => {
    if (err) {
      res.send({ error: 1, msg: '修改失败', err })
    } else {
      res.send({ error: 0, msg: '修改成功' })
    }
  })
})
module.exports = router
